package fr.emn.integration.synthese.controle;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.emn.integration.synthese.abstraction.Cours;

public class ControlJListEvaluation extends Observable implements Observer, ListSelectionListener {

	private JList liste;
	private Cours modele;

	public ControlJListEvaluation(Cours modele, JList liste) {
		this.modele = modele;
		this.liste = liste;
		String[] data = { "-" };
		this.liste.setListData(data);
		this.liste.setSelectedIndex(0);
	}

	public void update(Observable o, Object message) {
		Integer iMessage = (Integer) message;
		int i =0;
		if (iMessage == Cours.CHANGEMENT_MODULE_COURANT ||  iMessage == Cours.CHANGEMENT_EVALS) {
			
			int moduleCourant = this.modele.getModuleCourante();
			
			if (moduleCourant != -1) {
				i++;
				Vector<String> data = this.modele.getEvalDates(moduleCourant);
				data.add(0, "-");
				this.liste.setListData(data);
			} else {
				String[] data = { "-" };
				this.liste.setListData(data);
			}
			if (this.modele.getEvaluationCourante() != -1){
				this.liste.setSelectedValue(String.valueOf(this.modele.getEvaluationCourante()), true);}
			else{
				this.liste.setSelectedIndex(0);

			}
			this.liste.repaint();

		}
	}



	public void valueChanged(ListSelectionEvent e) {
		if (this.liste.getSelectedIndex() != -1
				&& !this.liste.getSelectedValue().equals("-"))
			this.modele.setEvaluationCourante(modele.findEvaluationId(this.liste.getSelectedValue().toString(), modele.getModuleCourante()));
		else if (this.liste.getSelectedIndex() != -1)
			this.modele.setEvaluationCourante(-1);
	}

}
